﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.Api.Biz
{
    public class GoodsService : ApiService
    {
        /// <summary>
        /// 标签配置
        /// </summary>
        /// <returns></returns>
        public void GetLabelConfig(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;



            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var config = dbcontext.ADZ_LabelConfig.Where(t => t.is_show == 1).OrderBy(t => t.sort_id).ToList();

            List<LabelConfigModel> labelConfigModels = new List<LabelConfigModel>();
            config.ForEach(t =>
            {
                LabelConfigModel labelConfig = new LabelConfigModel();
                labelConfig.title = t.label_name;
                labelConfig.isshow = t.is_show.Value;
                labelConfig.label_type = t.label_type;
                labelConfig.base_label_type = (int)t.base_label_type;

                if (t.label_type == 4 && t.is_show == 1)
                {
                    //检查有没有竞价的信息
                    if (dbcontext.ADZ_JingjiaConfig.FirstOrDefault(jingjia => jingjia.is_show == 1) == null)
                    {
                        labelConfig.isshow = 0;
                    }
                }
                if (labelConfig.isshow == 1)
                    labelConfigModels.Add(labelConfig);

            });

            var retobj = new
            {
                list = labelConfigModels
            };

            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(retobj);

            apiContext.apiHttpResp = apiResponse;
        }


        /// <summary>
        /// 品牌列表
        /// </summary>
        /// <returns></returns>
        public void GetBrandList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long category_id = dic.GetValue<long>("category_id", 0);
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);

            if (category_id == 0)
                category_id = -10;

            Tuple<bool, BrandModel> tupleResult = GoodsBiz.Instance.GetBrandList(-10, "", "", category_id, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "品牌为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 商品分类
        /// </summary>
        /// <returns></returns>
        public void GetCategory(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            long brand_id = -10;
            if (!string.IsNullOrWhiteSpace(data))
            {
                //解密后的json字符串反序列成对象
                SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
                brand_id = dic.GetValue<long>("brand_id", -10);
                if (brand_id == 0)
                    brand_id = -10;
            }

            var result = GoodsBiz.Instance.GetCategory(brand_id);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { list = result.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "商品分类为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 商品查询
        /// </summary>
        /// <returns></returns>
        public void GetList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int label_type = dic.GetValue<int>("label_type", -10);      //1 直播 2 预热 3 爆款
            int base_label_type = dic.GetValue<int>("base_label_type", -10);      //1 直播 2 预热 3 爆款
            if (base_label_type <= 0)
                base_label_type = -10;
            long brand_id = dic.GetValue<long>("brand_id", -10);        //品牌id
            long category_id = dic.GetValue<long>("category_id", -10);  //分类id
            string selKey = dic.GetValue<string>("selkey");             //搜索关键字
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);


            Tuple<bool, GoodsListModel> tupleResult = GoodsBiz.Instance.GetList(apiContext.CurrUserId, label_type, brand_id, category_id, selKey, pageindex, pagerows, 0, -10, base_label_type);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "商品为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取批量转发商品列表
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetBatchList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int label_type = dic.GetValue<int>("label_type", -10);      //1 直播 2 预热 3 爆款
            long brand_id = dic.GetValue<long>("brand_id", -10);        //品牌id
            long category_id = dic.GetValue<long>("category_id", -10);  //分类id
            string selKey = dic.GetValue<string>("selkey");             //搜索关键字
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);
            int base_label_type = dic.GetValue<int>("base_label_type", -10);      //1 直播 2 预热 3 爆款
            if (base_label_type <= 0)
                base_label_type = -10;
			if(pagerows > 100)
				pagerows = 100;
            //if (!EnumHelper.IsDefined<LabelType>(label_type))
            //{
            //    apiResponse.retCode = ResultEnum.ParamError;
            //    apiResponse.message = "参数错误";
            //    apiContext.apiHttpResp = apiResponse;
            //    return;
            //}

            Tuple<bool, GoodsListModel> tupleResult = GoodsBiz.Instance.GetBatchList(apiContext.CurrUserId, label_type, brand_id, category_id, selKey, pageindex, pagerows, base_label_type);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "商品为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 商品详情
        /// </summary>
        public void GetInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long goods_id = dic.GetValue<long>("goods_id", 0);

            if (goods_id <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, GoodsModel> tupleResult = GoodsBiz.Instance.GetInfo(apiContext.CurrUserId, goods_id);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "商品详情为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 价格查询
        /// </summary>
        public void QueryPrice(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string code = dic.GetValue<string>("code");
            string prodate = dic.GetValue<string>("prodate");

            if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(prodate))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (prodate.Length > 4 || !Utils.CheckNumber(prodate))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "生产日期格式错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = FinderBiz.Query(code, prodate);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(result.Item3);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 料号模糊查询
        /// </summary>
        public void QueryCode(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string code = dic.GetValue<string>("code");
            if (string.IsNullOrEmpty(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = FinderBiz.QueryCode(code);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { list = result.Item3 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 商品转发记录
        /// </summary>
        public void Forward(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string goods_ids = dic.GetValue<string>("goods_ids");       //转发商品id 多个商品id以逗号隔开
            int device_type = dic.GetValue<int>("device_type", 0);      //转发设备类型 1 ios 2 android
            string remark = dic.GetValue<string>("remark");             //转发内容 josn格式 非必填

            if (string.IsNullOrWhiteSpace(goods_ids) || device_type <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            if (!EnumHelper.IsDefined<DeviceType>(device_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "转发设备类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            var result = GoodsBiz.Instance.Forward(apiContext.CurrUserId, goods_ids, device_type, remark);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        #region 同步两边的物料信息

        /// <summary>
        /// 报名同步
        /// </summary>
        public void AsyncBaoming(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string goodsinfo = dic.GetValue<string>("goodsinfo", "");

            List<GoodsModel> goodslist = JsonHelper.Deserialize<List<GoodsModel>>(goodsinfo);

            var result = OrderBiz.Instance.AsyncStock(goodslist);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = result.Item2;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }
        #endregion
    }
}